// Test Unit
#include <stdio.h>
#include <stdlib.h>
#include "arraylist.h"
#include "linkedlist.h"
#include "linkedstack.h"

typedef int TYPE;		/* data type used in this test */

TYPE *NewData( TYPE value )		/* make room for a new data and set its value */
{
	TYPE *data;
	
	data = ( TYPE * ) malloc( sizeof( TYPE ) );
	*data = value;
	
	return data;
}

void TestArrayList() 
{
	ArrayList myList;
	ArrayListIterator it;
	
	TYPE *data, i;
	
	myList = AL_New( 10 );
	
	for ( i = 50; i < 60; i++ ) 
	{
		
		data = NewData( i );
		AL_Add( myList, AL_Begin( myList ), data );
		data = NewData( i );
		AL_Add( myList, AL_End( myList ), data );
	} 
	
	printf( "Array list:\n" );
	for ( it = AL_Begin( myList ); it != AL_End( myList ); it = AL_Next( myList, it ) ) 
	{
		printf( "%d ", *( TYPE * ) AL_Get( myList, it ) );
	}
	printf("\n\n");
	
	AL_Free(myList);
	myList = NULL;
}

void TestLinkedList( ) 
{
	LinkedList myList;
	LinkedListIterator it;
	
	TYPE *data, i;
	
	myList = LL_New( );
	
	for ( i = 30; i < 40; i++ )
	{
		data = NewData( i );
		LL_Add( myList, LL_End( myList ), data );
	}
	
	printf( "Doubly linked list:\n" );
	for ( it = LL_Begin( myList ); it != LL_End( myList ); it = LL_Next( myList, it ) )
	{
		printf("%d ", *( TYPE * ) LL_Get( myList, it ));
	}
	printf( "\n" );
	
	for ( it = LL_REnd( myList ); it != LL_RBegin( myList ); it = LL_Prev( myList, it ) ) 
	{
		printf("%d ", *( TYPE * ) LL_Get( myList, it ));
	}
	printf( "\n\n" );
	
	LL_Free( myList );
	myList = NULL;
}

void TestLinkedStack( ) 
{
	LinkedStack myStack;
	
	TYPE *data, i;
	
	myStack = LS_New( );
	
	printf( "Linked stack:\ndimension:%d\n", LS_Size( myStack ) );
	for ( i = 30; i < 40; i++ )
	{
		data = NewData( i );
		LS_Push( myStack, data );
		printf( "in top: %d\n", *( TYPE * ) LS_Top( myStack ) );
		if ( i < 35 )
		{
			printf( "pop: %d\n", *( TYPE * ) LS_Top( myStack ) );
			LS_Pop( myStack );
		}
	}
	
	printf( "dimension %d\nfinal stack configuration:\n", LS_Size( myStack ) );
	while( !LS_Empty( myStack ) )
	{
		printf("%d ", *( TYPE * ) LS_Top( myStack ));
		LS_Pop( myStack );
	}
	printf( "\n\n" );
	
	LS_Free( myStack );
	myStack = NULL;
}


int main( void )
{
	TestArrayList( );
	TestLinkedList( );
	TestLinkedStack( );
	
	return 0;
}
